package dmt16.TelemetrySuit.UI
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.GPSModule;
	import dmt16.TelemetrySuit.Controller.SensorTypes.SensorEvent;
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Tiles.TileBase;
	import dmt16.TelemetrySuit.Utils.NMEASentence;
	import dmt16.TelemetrySuit.Utils.StringUtils;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class MapView extends Sprite
	{
		public function MapView()
		{
			_text.embedFonts=true;
			_text.defaultTextFormat=_textFormat;
			_text.selectable=false;
			
			_btnTarget=new TargetBtn();
			_btnTarget.width=45;
			_btnTarget.height=45;
			_btnTarget.state=false;
			_btnTarget.addEventListener(MouseEvent.CLICK,_onUserCtrlClick);
					
			_map.addEventListener(Event.LOCATION_CHANGE,_onUserControl);
			
			_redraw();
			addChild(_map);
			addChild(_text);
			addChild(_btnTarget);
		}
		
		private function _onUserControl(event:Event):void{
			_btnTarget.state=true;
		}
		
		private var _sensor:GPSModule;
		public function get sensor():GPSModule{
			return _sensor;
		}
		public function attachSensor(sensor:GPSModule):void{
			if(_sensor!=null)
				_sensor.removeEventListener(SensorEvent.SENSOR_DATA_EVENT,_onSensorData);
			_sensor=sensor;
			_sensor.addEventListener(SensorEvent.SENSOR_DATA_EVENT,_onSensorData);
		}
		
		private var _mask:Shape=new Shape();
		private var _map:Map=new Map();
		private var _text:TextField=new TextField();
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigital;
			_textFormat.size=28;
			_textFormat.color=TileBase.FG_COLOUR;
		}
		
		private var _width:Number=100;
		public override function get width():Number{
			return _width;
		}
		public override function set width(value:Number):void{
			_width=value;
			_redraw();
		}
		
		private var _height:Number=250;
		public override function get height():Number{
			return _height;
		}
		public override function set height(value:Number):void{
			_height=value;
			_redraw();
		}
		
		private var _btnTarget:TargetBtn;
				
		private function _onSensorData(event:SensorEvent):void{
			var data:NMEASentence=_sensor.data[_sensor.data.length-1];
			_text.text="Time\t\t"+StringUtils.lpad(data.dateTime.hours.toString(),2)+":"+
				StringUtils.lpad(data.dateTime.minutes.toString(),2)+":"+
				StringUtils.lpad(data.dateTime.seconds.toString(),2)+
				"\rDate\t"+StringUtils.lpad(data.dateTime.date.toString(),2)+"/"+
				StringUtils.lpad(data.dateTime.month.toString(),2)+"/"+
				StringUtils.lpad(data.dateTime.fullYear.toString(),2)+
				"\r\rSpeed\t"+data.speed().toFixed(2)+" m/s\t\tAvg. "+_sensor.averageSpeed.toFixed(2)+" m/s"+
				"\rDistance\t"+_sensor.totalDistance.toFixed(2)+" m";
			_map.drawMapPath([data.position.x],[data.position.y]);			
		}
				
		private function _redraw():void{
			_mask.graphics.clear();
			_mask.graphics.beginFill(0);
			_mask.graphics.drawRect(0,0,_width,_height);
			_mask.graphics.endFill();
			
			graphics.clear();
			graphics.beginFill(TileBase.BG_COLOUR);
			graphics.lineStyle(1,TileBase.FG_COLOUR);
			graphics.drawRect(0,0,_width,_height);
			graphics.endFill();
			
			_map.width=_width;
			_map.height=_width;
			
			_text.width=_width-40;
			_text.height=_height-_width-40;
			_text.x=20;
			_text.y=_width+20;
			
			_btnTarget.x=_width-_btnTarget.width-20;
			_btnTarget.y=_width+20;
		}
		
		private function _onUserCtrlClick(event:MouseEvent):void{
			if(_map.userControl){
				_map.userControl=false;
				_btnTarget.state=false;
			}
			else{
				_map.userControl=true;
				_btnTarget.state=true;
				
			}
		}
	}
}